package panda.query.scan;

import panda.query.struct.Attribute;
import panda.query.struct.Constant;

/**
 * The scanner for a product query-tree node
 * @author Tian Yuan
 */
public class ProductScanner implements Scanner {
	
	Scanner l, r;
	
	public ProductScanner(Scanner l, Scanner r) {
		this.l = l;
		this.r = r;
		l.next();
	}

	public void init() {
		l.init();
		l.next();
		r.init();
	}
	
	public void close() {
		l.close();
		r.close();
	}

	public boolean next() {
		if (r.next())
			return true;
		else {
			r.init();
			return r.next() && l.next();
		}
	}

	public Constant getValue(Attribute attr) {
		if (l.hasAttribute(attr))
			return l.getValue(attr);
		else
			return r.getValue(attr);
	}

	public boolean hasAttribute(Attribute name) {
		return l.hasAttribute(name) || r.hasAttribute(name);
	}

}
